package jmeterBusiness;

import com.alibaba.fastjson.JSONArray;
import models.MyConfig;
import models.ReturnJson;
import models.TestServerBusinessImpl;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.Properties;

/**
 * Created by yangxianming on 2017/8/19.
 * 管理jmeterScript目录下的日志
 */
public class JmeterLogManager implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        String projectName = request.getParameter("projectName");
        String scriptName = request.getParameter("scriptName");
        Properties properties = myConfig.getPropertiesConfig().getProperties();
        String splitMark = myConfig.getSplitMark("splitMark");

        //获得jmeterScript目录下的html日志，返回目录列表
        if (Objects.equals("getHtmlList", request.getParameter("action"))){
            String jmxName = request.getParameter("jmxName");
            String jmeterScriptDir = myConfig.getAppPath() + "jmeterScript" + splitMark;
            JSONArray jtlHtmlList = new JSONArray();
            ArrayList<String> arrayList = new ArrayList<String>();
            File file = new File(jmeterScriptDir);
            for (File oneFile : file.listFiles()){
                if (oneFile.isDirectory()){
                    arrayList.add(oneFile.getName());
                }
            }
            Collections.sort(arrayList);
            for (int i = arrayList.size()-1; i >= 0; i--){
                jtlHtmlList.add(arrayList.get(i));
            }
            returnJson.setApiStatus(1);
            returnJson.setResult(jtlHtmlList);
        }

        return returnJson.toString();
    }

}
